#include "stdafx.h"
#include "FilterCombo.h"

namespace ui
{
	template<class ClassSelectMap>
	FilterComboTemplate<ClassSelectMap>::FilterComboTemplate(Layout* pLayout/* = new HLayout*/) :ComboBase<ClassSelectMap>(pLayout)
	{
		m_sControlClassName = L"FilterComboTemplate";
		m_pFilterComponent = new FilterListTemplate<ClassSelectMap>();
		m_pFilterComponent->SetListener(this);
	};

	template<class ClassSelectMap>
	bool FilterComboTemplate<ClassSelectMap>::CreateComboBody()
	{
		GlobalManager::FillBoxWithCache(this, GetComboXml());
		m_pImage = static_cast<Button*>(FindSubControl(L"image"));
		m_pRichEdit = static_cast<RichEdit*>(FindSubControl(L"input"));
		m_pBtnDropDown = static_cast<Button*>(FindSubControl(L"btn_drop_down"));
		if (m_bHideBtnDropDown)
			m_pBtnDropDown->SetVisible(false);

		if (!m_sReFontId.empty())
			m_pRichEdit->SetFont(m_sReFontId);
		if (!m_sReCaretColor.empty())
			m_pRichEdit->SetCaretColor(m_sReCaretColor);
		if (!m_sReTextColor.empty())
			m_pRichEdit->SetTextColor(m_sReTextColor);

		return true;
	}

	template<class ClassSelectMap>
	std::wstring FilterComboTemplate<ClassSelectMap>::GetText()
	{
		//return GetSelItemData().v;
		return m_pRichEdit->GetText();
	}
	template<class ClassSelectMap>
	void FilterComboTemplate<ClassSelectMap>::SetText(std::wstring sText)
	{
#ifdef _DEBUG
		if (!GetSelItemData().k.empty())
			assert(!"don't use FilterComboTemplate::SetText, please use FilterComboTemplate::SelectItem instend");
#endif
		m_bUpdatingData = true;
		m_pRichEdit->SetText(sText);
		m_bUpdatingData = false;
	}
	template<class ClassSelectMap>
	void FilterComboTemplate<ClassSelectMap>::UpdateData(bool bHideDropWnd)
	{
		if (!m_pFilterComponent)
			return;
		kvItem kv = m_pFilterComponent->GetSelItemData();
		std::wstring sText = kv.v;
		std::wstring sImage  =m_pFilterComponent->FindImageFileString(kv.img);
		m_pImage->SetBkImage(sImage);

		//ResetImageButtonState();
		
		
		m_bUpdatingData = true;
		m_pRichEdit->SetText(sText);
		m_bUpdatingData = false;
		printf("CFilterCombo::UpdateData\n");
		if (bHideDropWnd)
			m_pDropWnd->ShowDropWnd(false);
	}

	/*template<class ClassSelectMap>
	void FilterComboTemplate<ClassSelectMap>::BeforeHideDropWnd() {		
		kvItem kv = GetSelItemData();
		if (kv.k.empty())
			return;
		std::wstring sText = m_pRichEdit->GetText();
		if (sText.empty())
		{
			SetText(kv.v);
		}
	}*/

	template<class ClassSelectMap>
	void FilterComboTemplate<ClassSelectMap>::SelectItemCallback(bool bNotify) {
		UpdateData(true);
		if (m_pWindow && bNotify)
			m_pWindow->SendNotify(this, kEventSelect, 0, 0);
	}

	template class FilterComboTemplate<std::map<int, int>>;

} // namespace ui
